﻿--[[	$Id: init.lua 3515 2013-08-22 18:30:23Z sdkyron@gmail.com $	]]

local _, caelLib = ...

_G["caelLib"] = caelLib

caelLib.dummy = function() return end
caelLib.playerName = UnitName("player")
caelLib.playerClass = select(2, UnitClass("player"))
caelLib.playerRealm = GetRealmName()
caelLib.iLvl = math.floor(GetAverageItemLevel("player"))
caelLib.inCombat = UnitAffectingCombat("player")

caelLib.resolution = GetCVar("gxResolution")
caelLib.screenHeight = string.match(caelLib.resolution, "%d+x(%d+)")
caelLib.screenWidth = string.match(caelLib.resolution, "(%d+)x+%d")

caelLib.scales = {
	["720"] = { ["576"] = 0.65},
	["800"] = { ["600"] = 0.7},
	["960"] = { ["600"] = 0.84},
	["1024"] = { ["600"] = 0.89, ["768"] = 0.7},
	["1152"] = { ["864"] = 0.7},
	["1176"] = { ["664"] = 0.93},
	["1280"] = { ["800"] = 0.84, ["720"] = 0.93, ["768"] = 0.87, ["960"] = 0.7, ["1024"] = 0.65},
	["1360"] = { ["768"] = 0.93},
	["1366"] = { ["768"] = 0.93},
	["1440"] = { ["900"] = 0.84},
	["1600"] = { ["1200"] = 0.7, ["1024"] = 0.82, ["900"] = 0.93},
	["1680"] = { ["1050"] = 0.84},
	["1768"] = { ["992"] = 0.93},
	["1920"] = { ["1440"] = 0.7, ["1200"] = 0.84, ["1080"] = 0.93},
	["2048"] = { ["1536"] = 0.7},
	["2560"] = { ["1600"] = 0.84, ["1440"] = 0.93}, -- 1440 untested, suggested by Anj 0.83
}

caelLib.KillFrame = CreateFrame("Frame", nil, UIParent)
caelLib.KillFrame:Hide()

caelLib.kill = function(object)
    local objectReference = object

    if type(object) == "string" then
        objectReference = _G[object]
    else
        objectReference = object
    end

    if not objectReference then return end

	if objectReference.IsProtected then 
		if objectReference:IsProtected() then
			error("Attempted to kill a protected object: <"..objectReference:GetName()..">")
		end
	end

    if type(objectReference) == "frame" then
        objectReference:UnregisterAllEvents()
		objectReference:SetParent(caelLib.KillFrame)
    else
		objectReference.Show = caelLib.dummy
	end

    objectReference:Hide()
end

caelLib.utf8sub = function(string, numChars, dots)
	assert(string, "You need to provide a string to shorten. Usage: caelLib.utf8sub(string, numChars, includeDots)")
	assert(numChars, "You need to provide a length to shorten the string to. Usage: caelLib.utf8sub(string, numChars, includeDots)")

	local bytes = string:len()

	if bytes <= numChars then
		return string
	else
		local length, pos = 0, 1

		while pos <= bytes do
			length = length + 1

			local char = string:byte(pos)

			if char > 0 and char <= 127 then
				pos = pos + 1
			elseif char >= 192 and char <= 223 then
				pos = pos + 2
			elseif char >= 224 and char <= 239 then
				pos = pos + 3
			elseif char >= 240 and char <= 247 then
				pos = pos + 4
			end

			if length == numChars then
				break
			end
		end

		if length == numChars and pos <= bytes then
			return string:sub(1, pos - 1)..(dots and "..." or "")
		else
			return string
		end
	end
end

caelLib.isGuild = function(unit)
	local numGuildMembers = GetNumGuildMembers()

	for i = 1, numGuildMembers do
		local name = GetGuildRosterInfo(i)

		if name == unit then
			return true
		end
	end

	return false
end

caelLib.isInGroup = function(unit)
	if UnitPlayerOrPetInParty(unit) or UnitPlayerOrPetInRaid(unit) or UnitIsUnit(unit, "player") or UnitIsUnit(unit, "pet") then
		return true
	end

	return false
end

caelLib.isFriend = function(unit)
	for i = 1, select(2, BNGetNumFriends()) do
		for j = 1, BNGetNumFriendToons(i) do
			if unit == select(2, BNGetFriendToonInfo(i, j)) then
				return true
			end
		end
	end
			
	for i = 1, GetNumFriends() do
		if unit == GetFriendInfo(i) then
			return true
		end
	end
	
	return false
end

local SetUpAnimGroup = function(self)
	self.anim = self:CreateAnimationGroup("Flash")

	self.anim.fadeout = self.anim:CreateAnimation("ALPHA", "FadeOut")
	self.anim.fadeout:SetChange(0.75)
	self.anim.fadeout:SetOrder(1)

	self.anim.fadein = self.anim:CreateAnimation("ALPHA", "FadeIn")
	self.anim.fadein:SetChange(-0.75)
	self.anim.fadein:SetOrder(2)

	self.anim:SetLooping("BOUNCE")
end

caelLib.flash = function(self, duration)
	if not self.anim then
		SetUpAnimGroup(self)
	end

	if not self.anim:IsPlaying() or duration ~= self.anim.fadein:GetDuration() then
		self.anim.fadein:SetDuration(duration)
		self.anim.fadeout:SetDuration(duration)
		self.anim:Play()
	end
end

caelLib.stopflash = function(self)
	if self.anim then
		self.anim:Finish()
	end
end

local myToons = {
	["Illidan"] = {
		["Deathknot"]	=	"DEATHKNIGHT",

		["Ailuran"]		=	"DRUID",
		["Mahigan"]		=	"DRUID",
		["Therian"]		=	"DRUID",
		["Wachabe"]		=	"DRUID",

		["Caelash"]		=	"HUNTER", -- Orc 4, 6, 1, 1, 2
		["Caellian"]	=	"HUNTER", -- Night Elf 6, 1, 4, 5, 1

		["Archmagus"]	=	"MAGE", -- Pandaren 6, 7, 1, 1

		["Daruma"]		=	"MONK", -- Troll 6, 3, 5, 5, 6
		["Moopheus"]	=	"MONK", -- Tauren 1, 2, 3, 1, 1
		["Shaohao"]		=	"MONK", -- Pandaren 6, 7, 1, 1

		["Aequitas"]	=	"PALADIN", -- Blood Elf 6, 2, 6, 6, 6

		["Pythol"]		=	"PRIEST", -- Blood Elf 6, 2, 6, 6, 6

		["Decay"]		=	"ROGUE", -- Undead 1, 9, 1, 10, 11
		["Gorotsuki"]	=	"ROGUE", -- Night Elf 6, 1, 4, 5, 1
		["Kinjutsu"]	=	"ROGUE", -- Pandaren 6, 7, 1, 1
		["Kobalos"]		=	"ROGUE", -- Goblin 9, 3, 1, 3, 10
		["Ragnuk"]		=	"ROGUE", -- Goblin 9, 3, 1, 3, 10
		["Therian"]		=	"ROGUE", -- Worgen 3, 3, 4, 1, 1

		["Shenren"]		=	"SHAMAN", -- Draenei 4, 3, 9, 3, 1

		["Erebos"]		=	"WARLOCK", -- Orc 4, 6, 1, 1, 2

		["Exoskell"]	=	"WARRIOR", -- Undead 1, 9, 1, 10, 11
		["Pandahuan"]	=	"WARRIOR", -- Pandaren 6, 7, 1, 1
		["Qiulong"]		=	"WARRIOR", -- Pandaren 6, 7, 1, 1
	}
}

local herToons = {
	["Illidan"] = {
		["Mâjandrâ"]	=	"DRUID",

		["Liiam"]		=	"HUNTER",
		["Lliiam"]		=	"HUNTER",
		["Mâjandra"]	=	"HUNTER",
		["Penthésilée"]	=	"HUNTER",
		["Vâlandra"]	=	"HUNTER",

		["Maêjandra"]	=	"MONK",
		["Vaélendra"]	=	"MONK",

		["Malivaya"]	=	"PRIEST",

		["Callysto"]	=	"PRIEST",

		["Maajandra"]	=	"ROGUE",
		["Vàlàndrà"]	=	"ROGUE",

		["Pimicow"]		=	"SHAMAN",

		["Khaleesia"]	=	"WARLOCK",
		["Vaélandra"]	=	"WARLOCK",

		["Valimaya"]	=	"WARRIOR",
	}
}

caelLib.myToons = myToons[caelLib.playerRealm]
caelLib.myChars = myToons[caelLib.playerRealm] and myToons[caelLib.playerRealm][caelLib.playerName]
caelLib.herChars = herToons[caelLib.playerRealm] and herToons[caelLib.playerRealm][caelLib.playerName]

if (caelLib.myChars or caelLib.herChars) then
	function GetLocale()
		return "frFR"
	end
end